package main

import (
	"database/sql"
	"time"

	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
)

type Product struct {
	gorm.Model
	Code  string
	Price uint
}

type User struct {
	ID           uint
	Name         string
	Email        *string
	Age          uint8
	Birthday     *time.Time
	MemberNumber sql.NullString
	ActivatedAt  sql.NullTime
	CreatedAt    time.Time
	UpdatedAt    time.Time
	ignored      string
}

func main() {
	db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
	if err != nil {
		panic("failed to connect database")
	}

	// 数据库迁移
	db.AutoMigrate(&Product{})
	// 往数据库插入数据
	db.Create(&Product{
		Code:  "D42",
		Price: 100,
	})
	// 从数据库读取数据
	var product Product
	db.First(&product, 1)
	db.First(&product, "code = ?", "D42")
	db.Model(&product).Update("Price", 200)
	db.Model(&product).Updates(Product{Code: "F42", Price: 300})
	db.Model(&product).Updates(map[string]interface{}{"Code": "F44", "Price": 400})
	db.Delete(&product, 1)
}
